package com.china08.yxyapi.api.home.activity.healthcompetition;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudentAnswer;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentAnswerRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 点击提交按钮(计算成绩,修改答题状态)
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class YxyCompetitionStudentScoreUptController extends YxyApiBaseController {
    @Autowired
    private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;
    @Autowired
    private YxyCompetitionStudentAnswerRepository yxyCompetitionStudentAnswerRepository;

    @RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentscore/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        List<YxyCompetitionStudentAnswer> answerList = yxyCompetitionStudentAnswerRepository
                .findByStudentIdOrderByQuesOrder(reqModel.studentId);
        if (answerList.size() < 100) {
            respModel.notice = "提交失败,请先作答全部题目";
        } else {
            int score = 0;
            for (YxyCompetitionStudentAnswer answer : answerList) {
                if (answer.getMyAnswer().substring(0, 1).equals(answer.getQuesAnswer())) {
                    score++;
                }
            }
            YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
                    .findTop1ByStudentId(reqModel.studentId);
            if (yxyCompetitionStudent != null) {
                yxyCompetitionStudent.setScore(score);
                yxyCompetitionStudent.setStatus(2);
                yxyCompetitionStudentRepository.save(yxyCompetitionStudent);
                respModel.score = score;
                respModel.notice = "提交成功";
            }
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String studentId;// 学生id

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        // 分数
        public Integer score;
        // 提交结果提示
        public String notice;
    }
}
